package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.*;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.IdcardUtils;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.BaseCase;
import cn.fintecher.pangolin.entity.elastic.PersonalAddress;
import cn.fintecher.pangolin.entity.elastic.PersonalContact;
import cn.fintecher.pangolin.entity.elastic.PersonalImportModel;
import cn.fintecher.pangolin.service.dataimp.model.ConfirmModel;
import cn.fintecher.pangolin.service.dataimp.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:09 2019/11/5
 */

@Service("confirmCaseFuture")
public class ConfirmCaseFuture {
    @Autowired
    private BaseService baseService;

    /**
     * 多线程确认案件
     */
    @Async
    public CompletableFuture<List<ConfirmModel>> processCase(List<PersonalImportModel> personalImportModels, UserModel userModel) {
        List<ConfirmModel> list = new ArrayList<>();
        for (PersonalImportModel personalImportModel : personalImportModels) {
            String caseId = UUID.randomUUID().toString().replaceAll("-", ""); //客户信息ID
            //初始化对象
            ConfirmModel model = new ConfirmModel(); //案件确认对象
            BaseCase baseCase = generatePersonal(personalImportModel, caseId, userModel); //合同信息对象
            List<PersonalContact> personalContacts = generatePersonalContacts(personalImportModel, caseId, userModel, baseCase.getCaseNumber()); //客户联系人对象集合
            List<PersonalAddress> personalAddresses = generatePersonalAddress(personalImportModel, caseId, userModel, baseCase.getCaseNumber()); //客户地址信息集合
            model.setBaseCase(baseCase);
            model.setPersonalContacts(personalContacts);
            model.setPersonalAddresses(personalAddresses);
            list.add(model);
            personalImportModel.setFlag(1);
        }
        return CompletableFuture.completedFuture(list);
    }

    /**
     * 生成案件对象
     */
    private BaseCase generatePersonal(PersonalImportModel model, String caseId, UserModel userModel) {
        BaseCase baseCase = new BaseCase();
        baseCase.setId(caseId); //主键ID
        baseCase.setPrincipalId(model.getPrincipalId()); //委托方ID
        baseCase.setPrincipalName(model.getPrincipalName()); //委托方名称
        baseCase.setBatchNumber(model.getBatchNumber()); //批次号
        baseCase.setPersonalName(model.getName()); //客户姓名
        baseCase.setCertificateNo(IdcardUtils.conver15CardTo18(model.getCertificateNo())); //证件号
        baseCase.setCertificateType(model.getCertificateType()); //证件类型
        baseCase.setSex(IdcardUtils.getGenderByIdCard(model.getCertificateNo())); //性别
        baseCase.setBirthday(IdcardUtils.getBirthDateByIdCard(model.getCertificateNo())); //生日
        baseCase.setPhone(model.getSelfPhoneNo()); //移动电话
        baseCase.setHomePhoneNo(model.getHomePhoneNo()); //住宅电话
        baseCase.setEmployerPhoneNo(model.getEmployerPhoneNo()); //单位电话
        baseCase.setHomeAddr(model.getHomeAddr()); //住宅地址
        baseCase.setResidenceAddr(model.getResidenceAddr()); //户籍地址
        baseCase.setEmployerAddr(model.getEmployerAddr()); //单位地址
        baseCase.setEmployerName(model.getEmployerName()); //单位名称
        baseCase.setEmailAddr(model.getEmailAddr()); //邮箱地址
        baseCase.setBillAddr(model.getBillAddr()); //邮寄地址
        baseCase.setIsDistribute(BaseSelect.NO); //是否分配 否
        baseCase.setUnMapped(model.getUnMapped()); //未匹配字段
        baseCase.setExtensionAgent(model.getModel().getExtensionAgent());
        baseCase.setExtensionInfo(model.getModel().getExtensionInfo());
        baseCase.setRemindersDate(model.getModel().getRemindersDate());
        baseCase.setCaseNumber(model.getModel().getCaseNumber()); //案件编号
        baseCase.setDelegationDate(model.getModel().getDelegationDate()); //委案日期
        baseCase.setCity(model.getModel().getCity()); //委案城市
        baseCase.setOverduePeriods(model.getModel().getOverduePeriods()); //逾期期数
        baseCase.setOverdueDays(model.getModel().getOverdueDays()); //逾期天数
        baseCase.setAccount(model.getModel().getAccount()); //账户号
        baseCase.setCardNo(model.getModel().getCardNo()); //卡号
        baseCase.setCardNoType(model.getModel().getCardNoType()); //卡类型
        baseCase.setOpenCardDate(model.getModel().getOpenCardDate()); //开卡日期
        baseCase.setStopCardDate(model.getModel().getStopCardDate()); //停卡日期
        baseCase.setBillDay(model.getModel().getBillDay()); //账单日
        baseCase.setLimitAmt(model.getModel().getLimitAmt()); //卡额度
        baseCase.setEndCaseDate(model.getModel().getEndCaseDate());
        baseCase.setStage(model.getModel().getStage()); //委案阶段
        baseCase.setBranch(model.getModel().getBranch()); //分行
        baseCase.setSubBranch(model.getModel().getSubBranch()); //支行
        baseCase.setOverdueAmtTotal(model.getModel().getOverdueAmtTotal()); //委案金额
        baseCase.setLeftAmt(model.getModel().getLeftAmt()); //最新欠款金额
        baseCase.setLatestPayAmt(model.getModel().getLatestPayAmt()); //最后还款金额
        baseCase.setLatestPayDate(model.getModel().getLatestPayDate()); //最后还款日期
        baseCase.setIsClear(model.getModel().getLeftAmt() > 0 ? BaseSelect.NO : BaseSelect.YES); //是否结清
        baseCase.process(userModel.getId(), ZWDateUtil.getNowDateTime(), true);
        return baseCase;
    }

    /**
     * 生成客户联系人集合
     */
    private List<PersonalContact> generatePersonalContacts(PersonalImportModel model, String caseId, UserModel userModel, String caseNumber) {
        List<PersonalContact> list = new ArrayList<>();
        if (Objects.nonNull(model.getSelfPhoneNo())) { //本人电话不为空
            PersonalContact personalContact = setPersonalContactValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), model.getSelfPhoneNo(), PhoneType.MOBILE, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getHomePhoneNo())) { //本人住宅电话不为空
            PersonalContact personalContact = setPersonalContactValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), model.getHomePhoneNo(), PhoneType.HOME_PHONE, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getEmployerPhoneNo())) { //本人单位电话不为空
            PersonalContact personalContact = setPersonalContactValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), model.getEmployerPhoneNo(), PhoneType.OFFICE_PHONE, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo1())) { //联系人1电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation1()), caseId, model.getRemark1(), model.getPrincipalId(),
                    model.getCertificateNo1(), model.getName1(), model.getPhoneNo1(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo2())) { //联系人2电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation2()), caseId, model.getRemark2(), model.getPrincipalId(),
                    model.getCertificateNo2(), model.getName2(), model.getPhoneNo2(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo3())) { //联系人3电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation3()), caseId, model.getRemark3(), model.getPrincipalId(),
                    model.getCertificateNo3(), model.getName3(), model.getPhoneNo3(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo4())) { //联系人4电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation4()), caseId, model.getRemark4(), model.getPrincipalId(),
                    model.getCertificateNo4(), model.getName4(), model.getPhoneNo4(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo5())) { //联系人1电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation5()), caseId, model.getRemark5(), model.getPrincipalId(),
                    model.getCertificateNo5(), model.getName5(), model.getPhoneNo5(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo6())) { //联系人1电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation6()), caseId, model.getRemark6(), model.getPrincipalId(),
                    model.getCertificateNo6(), model.getName6(), model.getPhoneNo6(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        return list;
    }

    /**
     * 客户联系人对象赋值
     */
    private PersonalContact setPersonalContactValue(Relationship relationship, String caseId, String remark, String principalId,
                                                    String certificateNo, String name, String phoneNo, PhoneType type, UserModel userModel, String caseNumber) {
        PersonalContact personalContact = new PersonalContact();
        personalContact.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
        personalContact.setCaseId(caseId); //案件ID
        personalContact.setCaseNumber(caseNumber); //案件编号
        personalContact.setCertificateNo(certificateNo); //证件号
        personalContact.setName(name); //姓名
        personalContact.setRelation(relationship); //关系
        personalContact.setPhoneNo(phoneNo); //电话号码
        personalContact.setType(type); //电话类型
        personalContact.setRemark(remark); //备注
        personalContact.setSource(Source.IMPORT); //来源
        personalContact.setPrincipalId(principalId); //委托方ID
        personalContact.process(userModel.getId(), ZWDateUtil.getNowDateTime(), true);
        return personalContact;
    }

    /**
     * 生成客户地址信息集合
     */
    private List<PersonalAddress> generatePersonalAddress(PersonalImportModel model, String caseId, UserModel userModel, String caseNumber) {
        List<PersonalAddress> list = new ArrayList<>();
        if (Objects.nonNull(model.getHomeAddr())) { //本人住宅地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.HOUSE_ADDR, model.getHomeAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getResidenceAddr())) { //本人户籍地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.NATIVE_ADDR, model.getResidenceAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getEmployerAddr())) { //本人单位地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.COMPANY_ADDR, model.getEmployerAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getBillAddr())) { //本人邮寄地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.BILL_ADDR, model.getBillAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail1())) { //联系地址1不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation1()), caseId, model.getRemark1(), model.getPrincipalId(),
                    model.getCertificateNo1(), model.getName1(), baseService.addressTypeToEnum(model.getAddressType1()), model.getAddressDetail1(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail2())) { //联系地址2不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation2()), caseId, model.getRemark2(), model.getPrincipalId(),
                    model.getCertificateNo2(), model.getName2(), baseService.addressTypeToEnum(model.getAddressType2()), model.getAddressDetail2(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail3())) { //联系地址3不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation3()), caseId, model.getRemark3(), model.getPrincipalId(),
                    model.getCertificateNo3(), model.getName3(), baseService.addressTypeToEnum(model.getAddressType3()), model.getAddressDetail3(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail4())) { //联系地址4不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation4()), caseId, model.getRemark4(), model.getPrincipalId(),
                    model.getCertificateNo4(), model.getName4(), baseService.addressTypeToEnum(model.getAddressType4()), model.getAddressDetail4(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail5())) { //联系地址5不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation5()), caseId, model.getRemark5(), model.getPrincipalId(),
                    model.getCertificateNo5(), model.getName5(), baseService.addressTypeToEnum(model.getAddressType5()), model.getAddressDetail5(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail6())) { //联系地址6不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation6()), caseId, model.getRemark6(), model.getPrincipalId(),
                    model.getCertificateNo6(), model.getName1(), baseService.addressTypeToEnum(model.getAddressType6()), model.getAddressDetail6(), userModel, caseNumber);
            list.add(personalAddress);
        }
        return list;
    }

    /**
     * 客户地址对象赋值
     */
    private PersonalAddress setPersonalAddressValue(Relationship relationship, String caseId, String remark, String principalId,
                                                    String certificateNo, String name, AddressType type, String addressDetail, UserModel userModel, String caseNumber) {
        PersonalAddress personalAddress = new PersonalAddress();
        personalAddress.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
        personalAddress.setCaseId(caseId); //客户ID
        personalAddress.setCaseNumber(caseNumber); //案件编号
        personalAddress.setType(type); //地址类型
        personalAddress.setAddressDetail(addressDetail); //详细地址
        personalAddress.setCertificateNo(certificateNo); //证件号
        personalAddress.setName(name); //姓名
        personalAddress.setRelation(relationship); //关系
        personalAddress.setRemark(remark); //备注
        personalAddress.setPrincipalId(principalId); //委托方ID
        personalAddress.setSource(Source.IMPORT); //来源
        personalAddress.process(userModel.getId(), ZWDateUtil.getNowDateTime(), true);
        return personalAddress;
    }
}
